﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility;
using suduku;

namespace Sudukux
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }

        private void operateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TextBox[] box = new TextBox[256];
            //bound the textBox from left to right
            //1st row
            box[0] = textBox1;
            box[1] = textBox2;
            box[2] = textBox3;
            box[3] = textBox4;
            box[4] = textBox5;
            box[5] = textBox6;
            box[6] = textBox7;
            box[7] = textBox8;
            box[8] = textBox9;
            box[9] = textBox10;
            box[10] = textBox11;
            box[11] = textBox12;
            box[12] = textBox13;
            box[13] = textBox14;
            box[14] = textBox15;
            box[15] = textBox16;
            //2nd row
            box[16] = textBox32;
            box[17] = textBox31;
            box[18] = textBox30;
            box[19] = textBox29;
            box[20] = textBox28;
            box[21] = textBox27;
            box[22] = textBox26;
            box[23] = textBox25;
            box[24] = textBox24;
            box[25] = textBox23;
            box[26] = textBox22;
            box[27] = textBox21;
            box[28] = textBox20;
            box[29] = textBox19;
            box[30] = textBox18;
            box[31] = textBox17;
            //3rd row
            box[32] = textBox48;
            box[33] = textBox47;
            box[34] = textBox46;
            box[35] = textBox45;
            box[36] = textBox44;
            box[37] = textBox43;
            box[38] = textBox42;
            box[39] = textBox41;
            box[40] = textBox40;
            box[41] = textBox39;
            box[42] = textBox38;
            box[43] = textBox37;
            box[44] = textBox36;
            box[45] = textBox35;
            box[46] = textBox34;
            box[47] = textBox33;
            //4th row
            box[48] = textBox64;
            box[49] = textBox63;
            box[50] = textBox62;
            box[51] = textBox61;
            box[52] = textBox60;
            box[53] = textBox59;
            box[54] = textBox58;
            box[55] = textBox57;
            box[56] = textBox56;
            box[57] = textBox55;
            box[58] = textBox54;
            box[59] = textBox53;
            box[60] = textBox52;
            box[61] = textBox51;
            box[62] = textBox50;
            box[63] = textBox49;
            //5th row
            box[64] = textBox80;
            box[65] = textBox79;
            box[66] = textBox78;
            box[67] = textBox77;
            box[68] = textBox76;
            box[69] = textBox75;
            box[70] = textBox74;
            box[71] = textBox73;
            box[72] = textBox72;
            box[73] = textBox71;
            box[74] = textBox70;
            box[75] = textBox69;
            box[76] = textBox68;
            box[77] = textBox67;
            box[78] = textBox66;
            box[79] = textBox65;
            //6th row
            box[80] = textBox96;
            box[81] = textBox95;
            box[82] = textBox94;
            box[83] = textBox93;
            box[84] = textBox92;
            box[85] = textBox91;
            box[86] = textBox90;
            box[87] = textBox89;
            box[88] = textBox88;
            box[89] = textBox87;
            box[90] = textBox86;
            box[91] = textBox85;
            box[92] = textBox84;
            box[93] = textBox83;
            box[94] = textBox82;
            box[95] = textBox81;
            //7th row
            box[96] = textBox112;
            box[97] = textBox111;
            box[98] = textBox110;
            box[99] = textBox109;
            box[100] = textBox108;
            box[101] = textBox107;
            box[102] = textBox106;
            box[103] = textBox105;
            box[104] = textBox104;
            box[105] = textBox103;
            box[106] = textBox102;
            box[107] = textBox101;
            box[108] = textBox100;
            box[109] = textBox99;
            box[110] = textBox98;
            box[111] = textBox97;
            //8th row
            box[112] = textBox128;
            box[113] = textBox127;
            box[114] = textBox126;
            box[115] = textBox125;
            box[116] = textBox124;
            box[117] = textBox123;
            box[118] = textBox122;
            box[119] = textBox121;
            box[120] = textBox120;
            box[121] = textBox119;
            box[122] = textBox118;
            box[123] = textBox117;
            box[124] = textBox116;
            box[125] = textBox115;
            box[126] = textBox114;
            box[127] = textBox113;
            //9th row
            box[128] = textBox144;
            box[129] = textBox143;
            box[130] = textBox142;
            box[131] = textBox141;
            box[132] = textBox140;
            box[133] = textBox139;
            box[134] = textBox138;
            box[135] = textBox137;
            box[136] = textBox136;
            box[137] = textBox135;
            box[138] = textBox134;
            box[139] = textBox133;
            box[140] = textBox132;
            box[141] = textBox131;
            box[142] = textBox130;
            box[143] = textBox129;
            //10th row
            box[144] = textBox160;
            box[145] = textBox159;
            box[146] = textBox158;
            box[147] = textBox157;
            box[148] = textBox156;
            box[149] = textBox155;
            box[150] = textBox154;
            box[151] = textBox153;
            box[152] = textBox152;
            box[153] = textBox151;
            box[154] = textBox150;
            box[155] = textBox149;
            box[156] = textBox148;
            box[157] = textBox147;
            box[158] = textBox146;
            box[159] = textBox145;
            //11th row
            box[160] = textBox176;
            box[161] = textBox175;
            box[162] = textBox174;
            box[163] = textBox173;
            box[164] = textBox172;
            box[165] = textBox171;
            box[166] = textBox170;
            box[167] = textBox169;
            box[168] = textBox168;
            box[169] = textBox167;
            box[170] = textBox166;
            box[171] = textBox165;
            box[172] = textBox164;
            box[173] = textBox163;
            box[174] = textBox162;
            box[175] = textBox161;
            //12th row
            box[176] = textBox192;
            box[177] = textBox191;
            box[178] = textBox190;
            box[179] = textBox189;
            box[180] = textBox188;
            box[181] = textBox187;
            box[182] = textBox186;
            box[183] = textBox185;
            box[184] = textBox184;
            box[185] = textBox183;
            box[186] = textBox182;
            box[187] = textBox181;
            box[188] = textBox180;
            box[189] = textBox179;
            box[190] = textBox178;
            box[191] = textBox177;
            //13th row
            box[192] = textBox208;
            box[193] = textBox207;
            box[194] = textBox206;
            box[195] = textBox205;
            box[196] = textBox204;
            box[197] = textBox203;
            box[198] = textBox202;
            box[199] = textBox201;
            box[200] = textBox200;
            box[201] = textBox199;
            box[202] = textBox198;
            box[203] = textBox197;
            box[204] = textBox196;
            box[205] = textBox195;
            box[206] = textBox194;
            box[207] = textBox193;
            //14th row
            box[208] = textBox224;
            box[209] = textBox223;
            box[210] = textBox222;
            box[211] = textBox221;
            box[212] = textBox220;
            box[213] = textBox219;
            box[214] = textBox218;
            box[215] = textBox217;
            box[216] = textBox216;
            box[217] = textBox215;
            box[218] = textBox214;
            box[219] = textBox213;
            box[220] = textBox212;
            box[221] = textBox211;
            box[222] = textBox210;
            box[223] = textBox209;
            //15th row
            box[224] = textBox240;
            box[225] = textBox239;
            box[226] = textBox238;
            box[227] = textBox237;
            box[228] = textBox236;
            box[229] = textBox235;
            box[230] = textBox234;
            box[231] = textBox233;
            box[232] = textBox232;
            box[233] = textBox231;
            box[234] = textBox230;
            box[235] = textBox229;
            box[236] = textBox228;
            box[237] = textBox227;
            box[238] = textBox226;
            box[239] = textBox225;
            //16th row
            box[240] = textBox256;
            box[241] = textBox255;
            box[242] = textBox254;
            box[243] = textBox253;
            box[244] = textBox252;
            box[245] = textBox251;
            box[246] = textBox250;
            box[247] = textBox249;
            box[248] = textBox248;
            box[249] = textBox247;
            box[250] = textBox246;
            box[251] = textBox245;
            box[252] = textBox244;
            box[253] = textBox243;
            box[254] = textBox242;
            box[255] = textBox241;
            ArrayList list1 = new ArrayList();
            ArrayList list2 = new ArrayList();
            ArrayList list3 = new ArrayList();
            int k = 0, i, j;
            int sign = 0;
            for (i = 0; i < 256; i++)
            {
                if (box[i].Text != "")
                {
                    sign = 1;
                    k++;
                    list1.Add(i / 16 + 1);
                    list2.Add(i % 16 + 1);
                    list3.Add(Convert.ToInt32(box[i].Text));
                }
            }
            if (sign != 0)
            {
                int[,] dd = new int[k, 3];
                j = 0;
                for (i = 0; i < k; i++)
                {
                    dd[i, j] = (int)list1[i];
                }
                j = 1;
                for (i = 0; i < k; i++)
                {
                    dd[i, j] = (int)list2[i];
                }
                j = 2;
                for (i = 0; i < k; i++)
                {
                    dd[i, j] = (int)list3[i];
                }
                int mm = 4;
                suduku.sudoku su = new suduku.sudoku();
                //   MatrixS.MatrixS su1 = new MatrixS.MatrixS();
                MathWorks.MATLAB.NET.Arrays.MWNumericArray d = new MathWorks.MATLAB.NET.Arrays.MWNumericArray(dd);
                MathWorks.MATLAB.NET.Arrays.MWNumericArray m = (MathWorks.MATLAB.NET.Arrays.MWNumericArray)mm;
                MWArray S = su.sudokux(m, d);
                double[,] SInt = (double[,])S.ToArray();
                k = 0;
                for (i = 0; i < 16; i++)
                {
                    for (j = 0; j < 16; j++)
                    {
                        box[k++].Text = ((int)SInt[i, j]).ToString();
                    }
                }
                for (i = 0; i < 256; i++)
                {
                    box[i].ReadOnly = true;
                }
            }
            else
            {
                Warning w1 = new Warning();
                w1.ShowDialog();
            }
        }

        private void Form4_Load(object sender, EventArgs e)
        {

        }

        private void clearToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TextBox[] box = new TextBox[256];
            //bound the textBox from left to right
            //1st row
            box[0] = textBox1;
            box[1] = textBox2;
            box[2] = textBox3;
            box[3] = textBox4;
            box[4] = textBox5;
            box[5] = textBox6;
            box[6] = textBox7;
            box[7] = textBox8;
            box[8] = textBox9;
            box[9] = textBox10;
            box[10] = textBox11;
            box[11] = textBox12;
            box[12] = textBox13;
            box[13] = textBox14;
            box[14] = textBox15;
            box[15] = textBox16;
            //2nd row
            box[16] = textBox32;
            box[17] = textBox31;
            box[18] = textBox30;
            box[19] = textBox29;
            box[20] = textBox28;
            box[21] = textBox27;
            box[22] = textBox26;
            box[23] = textBox25;
            box[24] = textBox24;
            box[25] = textBox23;
            box[26] = textBox22;
            box[27] = textBox21;
            box[28] = textBox20;
            box[29] = textBox19;
            box[30] = textBox18;
            box[31] = textBox17;
            //3rd row
            box[32] = textBox48;
            box[33] = textBox47;
            box[34] = textBox46;
            box[35] = textBox45;
            box[36] = textBox44;
            box[37] = textBox43;
            box[38] = textBox42;
            box[39] = textBox41;
            box[40] = textBox40;
            box[41] = textBox39;
            box[42] = textBox38;
            box[43] = textBox37;
            box[44] = textBox36;
            box[45] = textBox35;
            box[46] = textBox34;
            box[47] = textBox33;
            //4th row
            box[48] = textBox64;
            box[49] = textBox63;
            box[50] = textBox62;
            box[51] = textBox61;
            box[52] = textBox60;
            box[53] = textBox59;
            box[54] = textBox58;
            box[55] = textBox57;
            box[56] = textBox56;
            box[57] = textBox55;
            box[58] = textBox54;
            box[59] = textBox53;
            box[60] = textBox52;
            box[61] = textBox51;
            box[62] = textBox50;
            box[63] = textBox49;
            //5th row
            box[64] = textBox80;
            box[65] = textBox79;
            box[66] = textBox78;
            box[67] = textBox77;
            box[68] = textBox76;
            box[69] = textBox75;
            box[70] = textBox74;
            box[71] = textBox73;
            box[72] = textBox72;
            box[73] = textBox71;
            box[74] = textBox70;
            box[75] = textBox69;
            box[76] = textBox68;
            box[77] = textBox67;
            box[78] = textBox66;
            box[79] = textBox65;
            //6th row
            box[80] = textBox96;
            box[81] = textBox95;
            box[82] = textBox94;
            box[83] = textBox93;
            box[84] = textBox92;
            box[85] = textBox91;
            box[86] = textBox90;
            box[87] = textBox89;
            box[88] = textBox88;
            box[89] = textBox87;
            box[90] = textBox86;
            box[91] = textBox85;
            box[92] = textBox84;
            box[93] = textBox83;
            box[94] = textBox82;
            box[95] = textBox81;
            //7th row
            box[96] = textBox112;
            box[97] = textBox111;
            box[98] = textBox110;
            box[99] = textBox109;
            box[100] = textBox108;
            box[101] = textBox107;
            box[102] = textBox106;
            box[103] = textBox105;
            box[104] = textBox104;
            box[105] = textBox103;
            box[106] = textBox102;
            box[107] = textBox101;
            box[108] = textBox100;
            box[109] = textBox99;
            box[110] = textBox98;
            box[111] = textBox97;
            //8th row
            box[112] = textBox128;
            box[113] = textBox127;
            box[114] = textBox126;
            box[115] = textBox125;
            box[116] = textBox124;
            box[117] = textBox123;
            box[118] = textBox122;
            box[119] = textBox121;
            box[120] = textBox120;
            box[121] = textBox119;
            box[122] = textBox118;
            box[123] = textBox117;
            box[124] = textBox116;
            box[125] = textBox115;
            box[126] = textBox114;
            box[127] = textBox113;
            //9th row
            box[128] = textBox144;
            box[129] = textBox143;
            box[130] = textBox142;
            box[131] = textBox141;
            box[132] = textBox140;
            box[133] = textBox139;
            box[134] = textBox138;
            box[135] = textBox137;
            box[136] = textBox136;
            box[137] = textBox135;
            box[138] = textBox134;
            box[139] = textBox133;
            box[140] = textBox132;
            box[141] = textBox131;
            box[142] = textBox130;
            box[143] = textBox129;
            //10th row
            box[144] = textBox160;
            box[145] = textBox159;
            box[146] = textBox158;
            box[147] = textBox157;
            box[148] = textBox156;
            box[149] = textBox155;
            box[150] = textBox154;
            box[151] = textBox153;
            box[152] = textBox152;
            box[153] = textBox151;
            box[154] = textBox150;
            box[155] = textBox149;
            box[156] = textBox148;
            box[157] = textBox147;
            box[158] = textBox146;
            box[159] = textBox145;
            //11th row
            box[160] = textBox176;
            box[161] = textBox175;
            box[162] = textBox174;
            box[163] = textBox173;
            box[164] = textBox172;
            box[165] = textBox171;
            box[166] = textBox170;
            box[167] = textBox169;
            box[168] = textBox168;
            box[169] = textBox167;
            box[170] = textBox166;
            box[171] = textBox165;
            box[172] = textBox164;
            box[173] = textBox163;
            box[174] = textBox162;
            box[175] = textBox161;
            //12th row
            box[176] = textBox192;
            box[177] = textBox191;
            box[178] = textBox190;
            box[179] = textBox189;
            box[180] = textBox188;
            box[181] = textBox187;
            box[182] = textBox186;
            box[183] = textBox185;
            box[184] = textBox184;
            box[185] = textBox183;
            box[186] = textBox182;
            box[187] = textBox181;
            box[188] = textBox180;
            box[189] = textBox179;
            box[190] = textBox178;
            box[191] = textBox177;
            //13th row
            box[192] = textBox208;
            box[193] = textBox207;
            box[194] = textBox206;
            box[195] = textBox205;
            box[196] = textBox204;
            box[197] = textBox203;
            box[198] = textBox202;
            box[199] = textBox201;
            box[200] = textBox200;
            box[201] = textBox199;
            box[202] = textBox198;
            box[203] = textBox197;
            box[204] = textBox196;
            box[205] = textBox195;
            box[206] = textBox194;
            box[207] = textBox193;
            //14th row
            box[208] = textBox224;
            box[209] = textBox223;
            box[210] = textBox222;
            box[211] = textBox221;
            box[212] = textBox220;
            box[213] = textBox219;
            box[214] = textBox218;
            box[215] = textBox217;
            box[216] = textBox216;
            box[217] = textBox215;
            box[218] = textBox214;
            box[219] = textBox213;
            box[220] = textBox212;
            box[221] = textBox211;
            box[222] = textBox210;
            box[223] = textBox209;
            //15th row
            box[224] = textBox240;
            box[225] = textBox239;
            box[226] = textBox238;
            box[227] = textBox237;
            box[228] = textBox236;
            box[229] = textBox235;
            box[230] = textBox234;
            box[231] = textBox233;
            box[232] = textBox232;
            box[233] = textBox231;
            box[234] = textBox230;
            box[235] = textBox229;
            box[236] = textBox228;
            box[237] = textBox227;
            box[238] = textBox226;
            box[239] = textBox225;
            //16th row
            box[240] = textBox256;
            box[241] = textBox255;
            box[242] = textBox254;
            box[243] = textBox253;
            box[244] = textBox252;
            box[245] = textBox251;
            box[246] = textBox250;
            box[247] = textBox249;
            box[248] = textBox248;
            box[249] = textBox247;
            box[250] = textBox246;
            box[251] = textBox245;
            box[252] = textBox244;
            box[253] = textBox243;
            box[254] = textBox242;
            box[255] = textBox241;
            int i;
            for (i = 0; i < 256; i++)
            {
                box[i].Text = "";
            }
            for (i = 0; i < 256; i++)
            {
                box[i].ReadOnly = false;
            }
        }

        private void sudokuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Help h1 = new Help();
            h1.ShowDialog();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About a1 = new About();
            a1.ShowDialog();
        }
    }
}
